package main

import (
	"encoding/json"
	"fmt"
	"net/http"
)

/*
help:
golang解析json格式//http://blog.csdn.net/qq_33204116/article/details/50700705
*/
func httpMessageDoWork(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	d := ClientJSONData{}
	if len(r.Form["c"]) > 0 {
		//log.Printf("\nhttpMessageDoWork 1c:" + r.Form["c"][0])
		d.C = string(r.Form["c"][0])
	} else {
		fmt.Fprint(w, `{"c":"error","d":"0"}`)
		return
	}
	if len(r.Form["d"]) > 0 {
		//log.Printf("\nhttpMessageDoWork 1d:" + r.Form["d"][0])
		d.D = string(r.Form["d"][0])
	}
	if len(r.Form["o"]) > 0 {
		//log.Printf("\nhttpMessageDoWork 1o:" + r.Form["o"][0])
		d.O = string(r.Form["o"][0])
	}
	if len(r.Form["p"]) > 0 {
		//log.Printf("\nhttpMessageDoWork 1p:" + r.Form["p"][0])
		d.P = string(r.Form["p"][0])
	}
	if len(r.Form["i"]) > 0 {
		//log.Printf("\nhttpMessageDoWork 1i:" + r.Form["i"][0])
		d.I = string(r.Form["i"][0])
	}
	if len(r.Form["e"]) > 0 {
		//log.Printf("\nhttpMessageDoWork 1e:" + r.Form["e"][0])
		d.E = string(r.Form["e"][0])
	}
	//MyPrintf("\nhttpMessageDoWork 2 http-get:: %s \n", d)
	messageDoWork(d, nil, w, 2)
}

func httpMessageSendToClient(d Message, w http.ResponseWriter) {
	//u := Message{P: "liamPost", D: "123", O: "time.Now()", I: "asd1234"}
	//fmt.Printf(w)
	// w.Header().Set("Access-Control-Allow-Origin", "*")
	// w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	// w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(d); err != nil {
		panic(err)
	}
	// else {
	// 	fmt.Fprint(w, "{\"C\":\""+d.C+"\",\"D\":\"-123\", \"O\":\"json error\"}")
	// }
}
